package com.yanqu.road.server.gameplayer.module.sacrifice;

import com.yanqu.road.logic.bussiness.player.UserSacrificeBussiness;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.sacrifice.UserSacrifice;
import com.yanqu.road.entity.sacrifice.UserSacrificePatronsData;
import com.yanqu.road.pb.sacrifice.SacrificeProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.sacrifice.SacrificeMgr;
import com.yanqu.road.server.pb.SacrificePb;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.LocalDateTimeHelper;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SacrificeModule extends GeneralModule {

    private UserSacrifice userSacrifice;

    private Map<Integer, UserSacrificePatronsData> userSacrificePatronsDataMap;

    public SacrificeModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        userSacrifice = UserSacrificeBussiness.getUserSacrifice(player.getUserId());
        initUserSacrifice();
        userSacrificePatronsDataMap = UserSacrificeBussiness.getUserSacrificePatronsDataMap(player.getUserId());
        checkUserSacrificePatronsMap();
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return false;
    }

    @Override
    public boolean saveData() {
        if(null != userSacrifice){
            if(userSacrifice.isInsertOption()){
                UserSacrificeBussiness.addUserSacrifice(userSacrifice);
            }else if(userSacrifice.isUpdateOption()){
                UserSacrificeBussiness.updateUserSacrifice(userSacrifice);
            }
        }
        for(UserSacrificePatronsData userSacrificePatronsData : userSacrificePatronsDataMap.values()){
            if(userSacrificePatronsData.isInsertOption()){
                UserSacrificeBussiness.addUserSacrificePatronsData(userSacrificePatronsData);
            }else if(userSacrificePatronsData.isUpdateOption()){
                UserSacrificeBussiness.updateUserSacrificePatronsData(userSacrificePatronsData);
            }
        }
        return true;
    }

    @Override
    public void afterLogin() {
        syncUserSacrifice();
        checkUserSacrificePatronsMap();
        syncUserSacrificePatronsData(new ArrayList<>(userSacrificePatronsDataMap.values()));
    }

    @Override
    public void loginSendMsg() {

    }

    public void resetOneDay(){
        if(null == userSacrifice){
            return;
        }
        long zeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp() / 1000;
        if(zeroTime > userSacrifice.getModifyTime()){
            userSacrifice.setTodayDamage(BigInteger.ZERO);
            userSacrifice.setAbilityRecordMark(0);
            userSacrifice.setModifyTime(zeroTime);
            syncUserSacrifice();
        }
        List<UserSacrificePatronsData> syncList = new ArrayList<>();
        for(UserSacrificePatronsData userSacrificePatronsData : userSacrificePatronsDataMap.values()){
            if(zeroTime > userSacrificePatronsData.getModifyTime()){
                userSacrificePatronsData.setBattleTimes(0);
                userSacrificePatronsData.setModifyTime(zeroTime);
                syncList.add(userSacrificePatronsData);
            }
        }
        if(syncList.size() > 0){
            syncUserSacrificePatronsData(syncList);
        }
    }

    private void initUserSacrifice(){
        if(null == userSacrifice){
            UserSacrifice tempSacrifice = new UserSacrifice();
            tempSacrifice.setUserId(player.getUserId());
            tempSacrifice.setModifyTime(System.currentTimeMillis() / 1000);
            tempSacrifice.setInsertOption();
            userSacrifice = tempSacrifice;
        }
    }

    public void syncUserSacrifice(){
        SacrificeProto.SacrificeDataMsg.Builder msg = SacrificePb.parseSacrificeDataMsg(userSacrifice);
        if(null != msg){
            player.sendPacket(Protocol.U_SACRIFICE_DATA_SYNC, msg);
        }
    }

    private void syncUserSacrificePatronsData(List<UserSacrificePatronsData> userSacrificePatronsDataList){
        player.sendPacket(Protocol.U_SACRIFICE_PATRONS_SYNC, SacrificePb.parseSacrificePatronsDataListMsg(player, userSacrificePatronsDataList));
    }

    public void syncUserSacrificePatronsData(UserSacrificePatronsData userSacrificePatronsData){
        if(null != userSacrificePatronsData){
            List<UserSacrificePatronsData> syncList = new ArrayList<>();
            syncList.add(userSacrificePatronsData);
            syncUserSacrificePatronsData(syncList);
        }
    }

    public void initSacrificeSystem(){
        checkUserSacrificePatronsMap();
        syncUserSacrificePatronsData(new ArrayList<>(userSacrificePatronsDataMap.values()));
        //系统解锁，记录玩家能力值
        SacrificeMgr.taskRecordPlayerInfo(player);
    }

    public void notifyAddPatrons(UserPatrons userPatrons){
        if(SystemOpenMgr.systemOpen(player, eSystemId.Sacrifice.getValue())) {
            UserSacrificePatronsData patronsData = getUserSacrificePatronsData(userPatrons.getPatronsId());
            if (null != patronsData) {
                syncUserSacrificePatronsData(patronsData);
            }
        }
    }

    public UserSacrificePatronsData getUserSacrificePatronsData(int patronsId){
        initUserSacrificePatronsData(patronsId);
        return userSacrificePatronsDataMap.get(patronsId);
    }

    private void checkUserSacrificePatronsMap(){
        if(SystemOpenMgr.systemOpen(player, eSystemId.Hunt.getValue()) || SystemOpenMgr.systemOpen(player, eSystemId.Sacrifice.getValue())){
            Map<Integer, UserPatrons> patronsMap = player.getModule(PatronsModule.class).getUserPatronsMap();
            for(UserPatrons userPatrons : patronsMap.values()){
                if(!userSacrificePatronsDataMap.containsKey(userPatrons.getPatronsId())){
                    initUserSacrificePatronsData(userPatrons.getPatronsId());
                }
            }
        }
    }

    private void initUserSacrificePatronsData(int patronsId) {
        if(!userSacrificePatronsDataMap.containsKey(patronsId) && player.getModule(PatronsModule.class).getUserPatronsMap().containsKey(patronsId)){
            UserSacrificePatronsData userSacrificePatronsData = new UserSacrificePatronsData();
            userSacrificePatronsData.setUserId(player.getUserId());
            userSacrificePatronsData.setPatronsId(patronsId);
            userSacrificePatronsData.setBattleTimes(0);
            userSacrificePatronsData.setModifyTime(System.currentTimeMillis() / 1000);
            userSacrificePatronsData.setInsertOption();
            userSacrificePatronsDataMap.put(userSacrificePatronsData.getPatronsId(), userSacrificePatronsData);
        }
    }

    public UserSacrifice getUserSacrifice() {
        return userSacrifice;
    }
}
